<template>
  <div>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="50%">
      <!-- 输入框 -->
      <el-form
        :model="userForm"
        status-icon
        :rules="rules"
        ref="userForm"
        label-width="100px"
        class="demo-userForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="userForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="userForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="userForm.email"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部确定按钮 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetForm">取 消</el-button>
        <el-button type="primary" @click="submitForm">提 交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addUser, updateUser } from "@/api";
export default {
  props: ["rowUserData", "title"],
  data() {
    var validateUsername = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名"));
      } else {
        callback();
      }
    };
    var validatePassword = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    return {
      dialogVisible: false,
      labelPosition: "right",
      userForm: {
        username: "",
        password: "",
        email: "",
        id: "",
      },
      rules: {
        username: [{ validator: validateUsername, trigger: "blur" }],
        password: [{ validator: validatePassword, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm() {
      this.$refs.userForm.validate(async (valid) => {
        if (valid) {
          // 添加商品
          if (this.title === "添加用户") {
            let { userForm } = this;
            let res = await addUser(userForm);
            if (res.status === 200) {
              this.$message({
                type: "success",
                message: "注册成功",
              });
              this.dialogVisible = false;
              this.resetForm();
              this.$parent.userList(1);
            } else {
              this.$message({
                type: "error",
                message: "注册失败",
              });
              return false;
            }
          } else {
            // 编辑用户信息
            let { userForm } = this;
            let res = await updateUser(userForm);
            if (res.status === 200) {
              this.dialogVisible = false;
              this.$parent.userList(1);
              this.resetForm();
              this.$message({
                type: "success",
                message: "修改用户信息成功",
              });
            } else {
              this.$message({
                type: "error",
                message: "修改用户信息失败",
              });
            }
          }
        } else {
          return false;
        }
      });
    },
    resetForm() {
      this.$refs.userForm.resetFields();
      this.dialogVisible = false;
      this.userForm = {
        username: "",
        password: "",
        email: "",
      };
    },
  },
  watch: {
    rowUserData(val) {
      this.userForm = this.rowUserData;
    },
  },
};
</script>

<style lang="less" scoped></style>
